package com.xumingxun.aibox.domain.node;

import com.xumingxun.aibox.ai.AiService;
import com.xumingxun.aibox.ai.impl.AiServiceImpl;
import com.xumingxun.aibox.context.Context;
import com.xumingxun.aibox.domain.ai.AiModel;
import com.xumingxun.aibox.domain.ai.Message;
import com.xumingxun.aibox.domain.var.Var;
import com.xumingxun.aibox.enums.NodeTypeEnum;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

@Getter
@Setter
public class AiNode extends Node{
    AiModel aiModel;
    String systemRole;
    String prompt;
    Integer historySize;
    boolean exportThink;

    Var answerVar;
    Var reasoningContentVar;

    AiService aiService;

    public AiNode(Context context) {
        super(context);
        init();
    }
    @Override
    public void init() {
        this.type = NodeTypeEnum.BRANCH;
        this.export = false;
        this.nodeName = "AI Node";
        answerVar=new Var(this.nodeName,"answer","AI 回答内容");
        reasoningContentVar=new Var(this.nodeName,"reasoning_content","思考过程");
        this.exportVars.add(answerVar);
        this.exportVars.add(reasoningContentVar);

        aiService=new AiServiceImpl();
        aiModel=new AiModel();
        aiModel.setId("1");
        aiModel.setMaxToken(1024);
        aiModel.setTemperature(0.7);
    }

    @Override
    public Node execute() {
        List<Message> messages=new ArrayList<>();
        messages.add(new Message("system",systemRole));
        messages.add(new Message("user",compileText(prompt)));

        Message message=aiService.chat(aiModel,messages);
        String content=message.getContent();

        context.setRuntimeVarValue(answerVar,content);

        return next;
    }


    @Override
    public Object getResult() {
        if(this.export){
            return context.getRuntimeVarValue(answerVar);
        }
        return null;
    }

}
